<?php

namespace app\lib\MessageBus\works;

use app\common\model\click\CpaClick;
use app\common\model\click\UserCallback;
use app\common\model\click\UserCallbackLog;
use app\common\model\promotion\PromotionLink;
use app\common\model\recycle_report\RecycleReport;
use app\common\model\return_rule\ReturnRule;
use app\common\model\risk_report\RiskReport;
use app\common\model\user\User;
use app\common\service\media\MediaService;
use app\lib\Instance;
use app\lib\MessageBus\base\AbstractWork;
use app\lib\MessageBus\EventType;
use GuzzleHttp\Client;
use think\facade\Log;


/**
 * 加桌回传
 *
 * https://open.oceanengine.com/labels/7/docs/1696710656359439
 *
 * Class UserActivateCallback
 * @package app\lib\MessageBus\works
 */
class UserActivateCallback extends AbstractWork
{
    public function handler($message)
    {
        dump("用户激活回调打印开始 用户ID-" . $message['user_id'] . date("Y-m-d H:i:s"), $message);
        $userCallback = UserCallback::where('user_id', $message['user_id'])->findOrEmpty();
        $returnRule   = ReturnRule::where('promotion_link_id', $message['promotion_link_id'])->findOrEmpty();
        // 后面可以放新的mq里面
        $time              = time();
        $promotion_link_id = $message['promotion_link_id'] ?? 0;

        if ($promotion_link_id) {
            $user = User::where('id',$message['user_id'])->findOrEmpty();
            PromotionLink::where(['id' => $message["promotion_link_id"]])->inc('add_desktop_user')->update();
            $userCallbackLog                     = UserCallbackLog::where(['user_id' => $message['user_id'], 'promotion_link_id' => $message["promotion_link_id"]])->order('dyeing_time desc')->findOrEmpty();
            $userCallbackLog->add_desktop_status = 1;
            $userCallbackLog->add_desktop_time   = $time;
            $userCallbackLog->save();
            // 判断当日推广链报表是否存在，不存在就创建
            $count_date = date('Y-m-d', $userCallbackLog->dyeing_time);
            $date       = date('Y-m-d', $time);
            $hour       = date('H');
            // 如果是当日加桌，就+1
            if ($date == $count_date) {
                RecycleReport::where(['count_date' => $count_date, 'promotion_link_id' => $message["promotion_link_id"]])->inc('add_desktop_num')->update();

                if (!RiskReport::where([
                    'count_date' => $count_date, 'promotion_link_id' => $message["promotion_link_id"], 'hour' => $hour
                ])->findOrEmpty()->isEmpty()) {
                    RiskReport::where([
                        'count_date' => $count_date, 'promotion_link_id' => $message["promotion_link_id"], 'hour' => $hour
                    ])->inc('add_desktop_num')->update();
                }
            }
        }

        if (!$returnRule->isEmpty()) {
            if ($returnRule->add_desktop == 1&&$user->is_new_user==0) {
                MediaService::addDesktop($message, $userCallback, $message['source'] ?? 0);
            } else {
                dump("规则不回传加桌信息 用户ID-" . $message['user_id']);
            }
        } else {
            dump("规则不存在 用户ID-" . $message['user_id']);
        }


        dump("用户激活回调打印结束 用户ID-" . $message['user_id'] . date("Y-m-d H:i:s"), $message);
    }

}
